﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SpWiki.Lib.Repository
{
    public class SpWikiLibrary
    {
        private static SPDocumentLibrary _documentLibrary;
        private const string SpWikiExt = ".SpWiki";

        private SpWikiLibrary() {}
        public SpWikiLibrary(SPDocumentLibrary documentLibrary)
        {
            _documentLibrary = documentLibrary;
        }


        /// <summary>
        /// Gets the sp wiki file by name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static SpWikiFile GetSpWikiFileByName(string name)
        {
            var fileName = name + SpWikiExt;
            var spFile = GetFile(null, fileName, "", null);
            return spFile != null ? new SpWikiFile(_documentLibrary.ID, spFile.Item.ID) : null;
        }

        /// <summary>
        /// Check if file the exists.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static bool FileExists(string name)
        {
            var fileName = name + SpWikiExt;
            var files = _documentLibrary.RootFolder.Files;
            return files.Cast<SPFile>().Any(file => file.Name == fileName);
        }

        /// <summary>
        /// Find a spfile, recursive.
        /// Choose only one of the parameters to search against
        /// <see cref="http://sphelper.codeplex.com/"/>
        /// </summary>
        /// <param name="folder">The folder.</param>
        /// <param name="fileName">leave null if not searching by fileName</param>
        /// <param name="documentTitle">leave null if not searching by title</param>
        /// <param name="uniqueId">leave null if not searching by uniqueId</param>
        /// <returns></returns>
        private static SPFile GetFile(SPFolder folder, string fileName, string documentTitle, Guid? uniqueId)
        {
            fileName = fileName.ToLower().Trim();
            SPFile file = null;
            if (folder == null) folder = _documentLibrary.RootFolder;

            // look in current folder for file

            if (folder != null && folder.Files != null && folder.Files.Count > 0)
            {
                foreach (SPFile spFile in folder.Files)
                {
                    if (!String.IsNullOrEmpty(fileName) && spFile.Name.ToLower().Trim() == fileName) { file = spFile; break; }
                    if (!String.IsNullOrEmpty(documentTitle) && spFile.Title.ToLower().Trim() == documentTitle) { file = spFile; break; }
                    if (uniqueId != null && spFile.UniqueId == (Guid)uniqueId) { file = spFile; break; }
                }
            }

            if (file != null) return file;

            // look in sub folders if not found

            if (folder != null && folder.SubFolders != null && folder.SubFolders.Count > 0)
            {
                foreach (SPFolder spFolder in folder.SubFolders)
                {
                    file = GetFile(spFolder, fileName, documentTitle, uniqueId);
                    if (file != null) { break; }
                }
            }

            return file;
        }

        /// <summary>
        /// Gets the edit new file URL.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public string GetEditNewFileUrl(string name)
        {
            var webUrl = _documentLibrary.ParentWeb.Site.MakeFullUrl(_documentLibrary.ParentWeb.ServerRelativeUrl);
            const string aspx = "/_layouts/SpWiki/Forms/Edit.aspx";
            var url = string.Concat(webUrl, "/", aspx, "?SpWikiList={", _documentLibrary.ID, "}&mode=new&name=", name);
            return url;
        }
    }
}
